import os
import textwrap
import time

from prettytable import PrettyTable
from rich.panel import Panel
from rich.table import Table

from data.config import full_path_val
from dll.dll_load import run

Console = full_path_val.FullPathBox.console


def wLine(cor=None, bg=None, val=None, place='center'):
    Console.rule(title=style_string(cor,bg,val), align=place)


def tableLine(cols, rows):
    table = Table()

    for i in cols:
        table.add_column(i)

    table.add_row(*rows)

    Console.print(table)


def panel(cor=None, bg=None, val=None):
    Console.print(Panel(style_string(cor,bg,val)))


def cls():
    os.system("cls")

def style_string(cor=None, bg=None, val=None):
    if val is None:
        val = "临时消息"

    if bg is not None:
        bg = f" on {bg}"
    else:
        bg = ""

    if cor is None:
        cor = "white"

    value = f"[{cor}{bg}]{val}"
    return value
def print_dict_as_column_table(data, page_size=10):
    keys = list(data.keys())

    current_page = 1
    start = (current_page - 1) * page_size
    end = current_page * page_size
    page_keys = keys[start:end]
    table = PrettyTable(page_keys)
    table.align = 'l'  # 左对齐
    for i in range(len(data[page_keys[0]])):
        table.add_row([data[key][i] for key in page_keys])
    print(table)


def color_print(cor=None, bg=None, val=None):
    Console.print(style_string(cor,bg,val))

def readKey() -> str:
    return run("ReadKey")

def Anykey():
    print("按任意键继续...")
    os.system("pause >> None")

def OneByOne(val, sleeptime=None):
    for i in val:
        print(i, end="")
        time.sleep(sleeptime if sleeptime is not None else 0.1)
